<?php 

//require_once("../database/sqlConn.php");
//require_once('../database/db_mysql.php');
require_once("../sqlConn.php");
$action=$_GET['action'];
switch($action)
{
	case 'AddActivity':
	   AddActivity($_POST['title'],$_POST['lastDays'],$_POST['maxNum'],$_POST['detailInfo'],$_POST['desc'],$_POST['userID']);
	   break;
	   
	case 'GetActivityList':
	   $activityList=GetActivityList();
	   echo $activityList;
	   break;
	   
	case 'GetiActivityList':
	   $activityList=GetiActivityList($_POST['userID']);
	   echo $activityList;
	   break;

	case 'GetfActivityList':
	   $activityList=GetfActivityList($_POST['userID']);
	   echo $activityList;
	   break;
	   
	case 'GetExpiredActivityList':
	   $activityList=GetExpiredActivityList();
	   echo $activityList;
	   break;

	case 'GetLeftTime':
	   $leftTimeStr=GetLeftTimeStr($_POST['createTime'],$_POST['lastDays']);
	   echo $leftTimeStr;
	   break;
	   
    case 'JoinActivity':
	   $joinRes=JoinActivity($_POST['activityId'],$_POST['userID'],$_POST['username']);
	   echo $joinRes;
	   break;
	
    case 'GetJoinNum':
	   $joinNum=GetJoinNum($_POST['activityId']);
	   echo $joinNum;
	   break;
	   
}


//发布新活动
function AddActivity($title,$lastDays,$maxNum,$detailInfo,$desc,$userID)
{
	//这句话我已经说很多次了 我希望 以后可以用session
	//session_start();
    //$userID= $_SESSION['userID'];
	//
	if($userID!=null && $userID!="")
	{
        connectDB();
	
	//PHP解释显示的时间为“格林威治标准时间”，与我们本地的时间相差8个小时。
	//date_default_timezone_set(’Asia/Shanghai’); 
    //或者修改php.ini里的 date.timezone = Asia/Chongqing 
	   $createTime=date("Y-m-d H:i:s");
	   $sql="insert into activity(title,userID,createTime,lastDays,maxNum,detailInfo,description)"
	 ."values('$title','$userID','$createTime','$lastDays','$maxNum','$detailInfo','$desc')";
	
		$res=mysql_query($sql);
		if($res)
		{
			echo "true";
			return;	
		}
		else
			return false;
	  //echo $res;

	}
	else
	{
	   echo	"请先登录";
	}
}

//获取活动列表
function GetActivityList()
{
	connectDB();
	
	//$sql="select id,title,createTime,lastDays,maxNum,joinNum from activity";
	$sql="select id,title,description,createTime,lastDays,maxNum,".
       "case when joinNum is null then 0 else joinNum  end as joinNum ".
       "from activity ".
       "left Join ".
       "(SELECT count(distinct userId ) AS joinNum, activityId FROM joinactivity group by activityId)as view1 ".  //又是传说中的权宜之计 周青张颀 我对不起你们
	  // "(SELECT count(distinct username ) AS joinNum, activityId FROM joinactivity group by activityId)as view1 ".
       "on activity.id=view1.activityId";
	   
	$res=mysql_query($sql);
	
	$activityList="[";					
	
	$count=0;
	
	while($row=mysql_fetch_array($res))
	{
		//将textarea中上传的文本中的换行符替换为"<br>"
		$title=str_replace("\r\n","<br>",$row["title"]);	
		$title=str_replace("\n","<br>",$title);	
		
		//获取剩余时间
		$leftTime=GetLeftTimeStr($row["createTime"],$row["lastDays"]);
		//echo  " $leftTime<br>";	
		if($leftTime!=null)
		{

	    	$rowStr="{id:'".$row["id"]."',title:'".$title."',desc:'".$row["description"]."',createTime:'".$row["createTime"]."',lastDays:'".$row["lastDays"]."',leftTime:'".$leftTime."',maxNum:'".$row["maxNum"]."',joinNum:'".$row["joinNum"]."'},";
		    $activityList.=$rowStr;	
			
			$count++;
		}
	}
	if($count>0)
	{
		 $activityList=substr_replace($activityList,'',-1,1);
	}
	$activityList.="]";
	
	return $activityList;

}
//获取我参与的活动列表
function GetiActivityList($userID)
{
	connectDB();
	
	//$sql="select id,title,createTime,lastDays,maxNum,joinNum from activity";
	$sql="select a.id,title,description,createTime,lastDays,maxNum,".
       "case when joinNum is null then 0 else joinNum  end as joinNum ".
       "from activity a ".
       "left Join ".
       "(SELECT count(distinct userId ) AS joinNum, activityId FROM joinactivity group by activityId)as view1 ". 
       "on a.id=view1.activityId ".
       "LEFT JOIN joinactivity ON a.id=joinactivity.activityID where joinactivity.userID='$userID'";
	   
	$res=mysql_query($sql);
	
	echo mysql_error();

	$activityList="[";					
	
	$count=0;
	
	while($row=mysql_fetch_array($res))
	{
		//将textarea中上传的文本中的换行符替换为"<br>"
		$title=str_replace("\r\n","<br>",$row["title"]);	
		$title=str_replace("\n","<br>",$title);	
		
		//获取剩余时间
		$leftTime=GetLeftTimeStr($row["createTime"],$row["lastDays"]);
		//echo  " $leftTime<br>";	
		if($leftTime!=null)
		{

	    	$rowStr="{id:'".$row["id"]."',title:'".$title."',desc:'".$row["description"]."',createTime:'".$row["createTime"]."',lastDays:'".$row["lastDays"]."',leftTime:'".$leftTime."',maxNum:'".$row["maxNum"]."',joinNum:'".$row["joinNum"]."'},";
		    $activityList.=$rowStr;	
			
			$count++;
		}
	}
	if($count>0)
	{
		 $activityList=substr_replace($activityList,'',-1,1);
	}
	$activityList.="]";
	
	return $activityList;

}

//获取我参与的活动列表
function GetfActivityList($userID)
{
	connectDB();
	
	//$sql="select id,title,createTime,lastDays,maxNum,joinNum from activity";
	$sql="select a.id,title,description,createTime,lastDays,maxNum,".
       "case when joinNum is null then 0 else joinNum  end as joinNum ".
       "from activity a ".
       "left Join ".
       "(SELECT count(distinct userId ) AS joinNum, activityId FROM joinactivity group by activityId)as view1 ". 
       "on a.id=view1.activityId ".
       "inner JOIN joinactivity ON a.id=joinactivity.activityID ";
       "INNER JOIN friend where joinactivity.userID=friend.frid and friend.foid='$userID' ";	   
	$res=mysql_query($sql);
	
	echo mysql_error();

	$activityList="[";					
	
	$count=0;
	
	while($row=mysql_fetch_array($res))
	{
		//将textarea中上传的文本中的换行符替换为"<br>"
		$title=str_replace("\r\n","<br>",$row["title"]);	
		$title=str_replace("\n","<br>",$title);	
		
		//获取剩余时间
		$leftTime=GetLeftTimeStr($row["createTime"],$row["lastDays"]);
		//echo  " $leftTime<br>";	
		if($leftTime!=null)
		{

	    	$rowStr="{id:'".$row["id"]."',title:'".$title."',desc:'".$row["description"]."',createTime:'".$row["createTime"]."',lastDays:'".$row["lastDays"]."',leftTime:'".$leftTime."',maxNum:'".$row["maxNum"]."',joinNum:'".$row["joinNum"]."'},";
		    $activityList.=$rowStr;	
			
			$count++;
		}
	}
	if($count>0)
	{
		 $activityList=substr_replace($activityList,'',-1,1);
	}
	$activityList.="]";
	
	return $activityList;

}

//获取过期活动列表
//将来活动多了 可能要建两个表 把过期的活动移出来 查到一个专门的过期活动表里
//这样就不需要每次都计算了
function GetExpiredActivityList()
{
	connectDB();
	
	//$sql="select id,title,createTime,lastDays,maxNum,joinNum from activity";
	$sql="select id,title,description,createTime,lastDays,maxNum,".
       "case when joinNum is null then 0 else joinNum  end as joinNum ".
       "from activity ".
       "left Join ".
       "(SELECT count(distinct userId ) AS joinNum, activityId FROM joinactivity group by activityId)as view1 ".  //又是传说中的权宜之计 周青张颀 我对不起你们
	  // "(SELECT count(distinct username ) AS joinNum, activityId FROM joinactivity group by activityId)as view1 ".
       "on activity.id=view1.activityId";
	   
	$res=mysql_query($sql);
	
	$activityList="[";					
	
	$count=0;
	
	while($row=mysql_fetch_array($res))
	{
		//将textarea中上传的文本中的换行符替换为"<br>"
		$title=str_replace("\r\n","<br>",$row["title"]);	
		$title=str_replace("\n","<br>",$title);	
		//将textarea中上传的文本中的换行符替换为"<br>"
		$desc=str_replace("\r\n","<br>",$row["description"]);	
		$desc=str_replace("\n","<br>",$desc);	
		
		//获取剩余时间
		$leftTime=GetLeftTimeStr($row["createTime"],$row["lastDays"]);
		//echo  " $leftTime<br>";	
		if($leftTime==null)
		{

	    	$rowStr="{id:'".$row["id"]."',title:'".$title."',desc:'".$desc."',createTime:'".$row["createTime"]."',lastDays:'".$row["lastDays"]."',leftTime:'".$leftTime."',maxNum:'".$row["maxNum"]."',joinNum:'".$row["joinNum"]."'},";
		    $activityList.=$rowStr;	
			
			$count++;
		}
	}
	if($count>0)
	{
		 $activityList=substr_replace($activityList,'',-1,1);
	}
	$activityList.="]";
	
	return $activityList;

}

//根据活动Id，获取此活动已参加的人数
function GetJoinNum($activityId)
{
	connectDB();
	//$sql="SELECT count(distinct userId) as joinNum from joinActivity where activityID=$activityId"; 有些人伤不起啊
	$sql="SELECT count(distinct username) as joinNum from joinActivity where activityID=$activityId";
	$res=mysql_query($sql);		
		
	if($row=mysql_fetch_array($res))
	{
       return $row[0];
	}
}

//获得剩余时间的字符串
function GetLeftTimeStr($createTime,$lastDays)
{
	 $now=date("Y-m-d H:i:s");
     $leftTime=strtotime("$createTime +$lastDays days")-strtotime($now);
		/*echo "createTime=$createTime<br>";
		echo "lastDays=$lastDays<br>";
		echo "now=$now<br>";
		echo "left=$leftTime<br>";*/
		
     if($leftTime>0)
	 {
		$leftDay=floor($leftTime/(3600*24));
		$modeDay=$leftTime%(3600*24);
		$leftHour=floor($modeDay/3600);
		$modeHour=$modeDay%3600;
		$leftMin=floor($modeHour/60);
		$leftSec=$modeHour%60;
		
		$leftTimeStr=$leftDay."天".$leftHour."小时".$leftMin."分".$leftSec."秒";
	 }
	 //已过期
	 else
	 {
		 $leftTimeStr=null;
	 }
	 return $leftTimeStr;
}


//用户加入活动
function JoinActivity($activityId,$userID,$username)
{
	//session_start();
	//$userId=$_SESSION['_userID'];
	//用户未登录
	//if($userId==null || $userId=="")
 	//{
	//	return "NotLogin"; //这个 session的使用 今后再考虑
	//}//注释掉你 只是权宜之计
	if($userID==null || $userID=="" || $username==null || $username=="")//kakassi add 通过username cookie做session的代替品
 	{
		return "NotLogin"; //这个 session的使用 今后再考虑
	}
	//用户已登录
	else
	{
		//虽然过期的话 服务器不可能收到这个请求 但是出于安全性考虑 还是加一下时间验证
		//算了 先不加了 因为 客户端要伪造消息加入一个已经过期的活动也没有意义 
		//既然客户端没有驱动力去做这件事情 我为什么要去防止呢
		//
		connectDB();
	    $joinTime=date("Y-m-d H:i:s");
		//$sql="select id from joinActivity where userID=$userId and activityID=$activityId";
		//将来会改成id的 或者说 恢复成id的 我只不顾试验一下
		$sql="select id from joinactivity where userID='$userID' and username='$username' and activityID='$activityId'";
		$res=mysql_query($sql);
		//用户已参与
		if($res)
		{
			if(mysql_fetch_array($res))
			{
				return "您已加入此活动！";			
			}
	
	        //$sql="insert into joinActivity(userID,activityID,joinTime)"
	        //."values('$userId','$activityId','$joinTime')";
		    $sql="insert into joinActivity(userID,username,activityID,joinTime)"
	        ."values('$userID','$username','$activityId','$joinTime')";
	       $res=mysql_query($sql);
	       if($res)
	       {
		     return "加入成功！";
	       }
		}
	}
}
?>
